package com.lemon.cloud.generator.web;

import cn.hutool.core.io.IoUtil;
import cn.hutool.http.ContentType;
import cn.smallbun.screw.boot.config.Screw;
import cn.smallbun.screw.boot.properties.ScrewProperties;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.core.utils.SpringContextHolder;
import com.lemon.cloud.generator.entity.GenDatasourceConf;
import com.lemon.cloud.generator.service.GenDatasourceConfService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import jakarta.validation.Valid;
import java.util.List;

/**
 * 数据源管理
 */
@RestController
@RequestMapping("dsconf")
public class GenDsConfController {

    @Autowired
    private GenDatasourceConfService datasourceConfService;

    @Autowired
    private Screw screw;
    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param datasourceConf 数据源表
     * @return
     */
    @GetMapping("/page")
    public ResultMsg<IPage<GenDatasourceConf>> getSysDatasourceConfPage(Page page, GenDatasourceConf datasourceConf) {
        return ResultMsg.resultSuccess(datasourceConfService.page(page, Wrappers.query(datasourceConf)));
    }

    /**
     * 查询全部数据源
     *
     * @return
     */
    @GetMapping("/list")
    public ResultMsg<List<GenDatasourceConf>> list() {
        return ResultMsg.resultSuccess(datasourceConfService.list());
    }

    @PostMapping
    public ResultMsg save(@RequestBody @Valid  GenDatasourceConf datasourceConf){
        return ResultMsg.resultSuccess(datasourceConfService.saveDsByEnc(datasourceConf));
    }

    @PutMapping
    public ResultMsg update(@RequestBody @Valid  GenDatasourceConf datasourceConf){
        return ResultMsg.resultSuccess(datasourceConfService.updateDsByEnc(datasourceConf));
    }

    @DeleteMapping("/{id}")
    public ResultMsg removeById(@PathVariable("id") Long id) {
        return ResultMsg.resultSuccess(datasourceConfService.removeByDsId(id));
    }


    /**
     * 查询数据源对应的文档
     * @param dsName 数据源名称
     */
    @SneakyThrows
    @GetMapping("/doc")
    public void generatorDoc(String dsName, HttpServletResponse response) {
        // 设置指定的数据源
        DynamicRoutingDataSource dynamicRoutingDataSource = SpringContextHolder.getBean(DynamicRoutingDataSource.class);
        DynamicDataSourceContextHolder.push(dsName);
        DataSource dataSource = dynamicRoutingDataSource.determineDataSource();

        // 设置指定的目标表
        ScrewProperties screwProperties = SpringContextHolder.getBean(ScrewProperties.class);

        // 生成
        byte[] data = screw.documentGeneration(dataSource, screwProperties).toByteArray();
        response.reset();
        response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length));
        response.setContentType(ContentType.OCTET_STREAM.getValue());
        IoUtil.write(response.getOutputStream(), Boolean.TRUE, data);
    }
}
